package com.ldc.common.mvvm;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewbinding.ViewBinding;

public abstract class BaseMvvmFragment<VB extends ViewBinding, VM extends ViewModelBase> extends BaseFragment<VB> {
    protected VM viewModel;

    protected abstract VM initViewModel();

    @Override
    protected VB loadViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        VB vb = super.loadViewBinding(inflater, container, savedInstanceState);
        viewModel = initViewModel();
        if (null != viewModel) {
            getLifecycle().addObserver(viewModel);
        }
        return vb;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != viewModel) {
            getLifecycle().removeObserver(viewModel);
        }
    }

}
